In [1]:
# Author: Stephen Situ
# MongoDB is a popular document based NoSQL database that is known for its scalability and flexible schema. The schema of
# MongoDB does not have to be specified and you can have fields/values that only apply to some documents in a collection. 
# It also has replication and high availability built in. In this project, we use the pymongo library in Python to connect to 
# a MongoDB database and practice CRUD operations in the database. 
In [5]:
# importing MangoClient
from pymongo import MongoClient
In [6]:
# setting cluster
cluster = MongoClient("mongodb+srv://Joe:1234@cluster0.lz7wlnd.mongodb.net/test?retryWrites=true&w=majority")
In [7]:
# setting database
db = cluster["test"]
In [8]:
# setting collection
collection = db["todos"]
In [13]:
# Use python dictionary as a document, if no value given for _id, random object ID is given instead
post = {"_id":0, "name": "tim", "score":5}
In [14]:
# To add document use insert_one and insert_many
collection.insert_one(post)
results = collection.find({})
for result in results:
    print(result)
{'_id': 0, 'name': 'tim', 'score': 5}
In [16]:
# We Can define multiple posts
post1 = {"_id":5, "name":"joe"}
post2 = {"_id":6, "name":"bill"}
In [17]:
# Use insert_many
collection.insert_many([post1,post2])
results = collection.find({})
for result in results:
    print(result)
{'_id': 0, 'name': 'tim', 'score': 5}
{'_id': 5, 'name': 'joe'}
{'_id': 6, 'name': 'bill'}
In [18]:
# Using find_one (only first result) and find (all results) query to filter
results = collection.find({"name":"bill"})
for result in results:
    print(result)
{'_id': 6, 'name': 'bill'}
In [19]:
# if only want one field like "_id" in results
results = collection.find({"name":"bill"})
for result in results:
    print(result["_id"])
6
In [21]:
# Looking for multiple fields (both need to match)
results = collection.find({"name":"tim","_id":0})
for result in results:
    print(result)
{'_id': 0, 'name': 'tim', 'score': 5}
In [22]:
# Using find_one to only find one entry
results = collection.find_one({"name":"tim","_id":0})
print(results)
{'_id': 0, 'name': 'tim', 'score': 5}
In [23]:
# To return everything, use empty dictionary
results = collection.find({})
for result in results:
    print(result)
{'_id': 0, 'name': 'tim', 'score': 5}
{'_id': 5, 'name': 'joe'}
{'_id': 6, 'name': 'bill'}
In [24]:
# Using delete_one & delete_many to delete document
collection.delete_one({"_id":0})
results = collection.find({})
for result in results:
    print(result)
{'_id': 5, 'name': 'joe'}
{'_id': 6, 'name': 'bill'}
In [25]:
# to update, use update_one & update_many with $set operator
collection.update_one({"_id":5,}, {"$set":{"name":"tim"}})
results = collection.find({})
for result in results:
    print(result)
{'_id': 5, 'name': 'tim'}
{'_id': 6, 'name': 'bill'}
In [26]:
# to add a new field to a document, simply use $set and add the new field
collection.update_one({"_id":5,}, {"$set":{"pet":"cat"}})
results = collection.find({})
for result in results:
    print(result)
{'_id': 5, 'name': 'tim', 'pet': 'cat'}
{'_id': 6, 'name': 'bill'}
In [27]:
# can use replace_one and replace_many to replace all entries in document
collection.replace_one({"_id":6},{"identification":60})
results = collection.find({})
for result in results:
    print(result)
{'_id': 5, 'name': 'tim', 'pet': 'cat'}
{'_id': 6, 'identification': 60}
In [39]:
# Can use aggragate functions like count
# Count all
post_count = collection.count_documents({})
print(post_count)
2
In [40]:
# specific criteria
post_count = collection.count_documents({"_id":5})
print(post_count)
1